// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueLazyload from 'vue-lazyload'
import VueScroller from 'vue-scroller'
import VueTouch from 'vue-touch'
import FastClick from 'fastclick'
import router from './router'
import App from './App'
import store from './store'
import './styles/index.less'

Vue.use(VueLazyload)
Vue.use(VueScroller)
Vue.use(VueTouch, {name: 'v-touch'})

VueTouch.config.swipe = {
  threshold: 260 //手指左右滑动距离
}

import {Cell, Group, Loading} from 'vux'

Vue.component('cell', Cell)
Vue.component('group', Group)
Vue.component('loading', Loading)


import geturlpara from './util/MyUtil'
import HttpUtil from './util/HttpUtil'

Vue.prototype.$geturlpara = geturlpara
Vue.prototype.$httputil = HttpUtil

/*引入移动端手势库*/
import directives from './directives/touch'

directives(Vue);


FastClick.attach(document.body)

Vue.config.productionTip = false


const history = window.sessionStorage;
history.clear()
let historyCount = history.getItem('count') * 1 || 0;
history.setItem('/', 0);

router.beforeEach(function (to, from, next) {
  const toIndex = history.getItem(to.path);
  const fromIndex = history.getItem(from.path);
  if (toIndex) {
    if (!fromIndex || parseInt(toIndex, 10) > parseInt(fromIndex, 10) || (toIndex === '0' && fromIndex === '0')) {
      store.commit('updateDirection', {direction: 'forward'})
    } else {
      store.commit('updateDirection', {direction: 'reverse'})
    }
  } else {
    ++historyCount;
    history.setItem('count', historyCount);
    to.path !== '/' && history.setItem(to.path, historyCount);
    store.commit('updateDirection', {direction: 'forward'})
  }
  store.commit('updateLoadingStatus', {isLoading: true})
  next()
})

router.afterEach(function (to) {
  store.commit('updateLoadingStatus', {isLoading: false})
})


/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app-box')
